<?php
namespace App\Http\Controllers\Admin\Product;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use PHPUnit\Framework\MockObject\Stub\Exception;

class ProductController extends BaseController
{
    protected  $api_url = [
        'index' => ['get', 'product'],
        'show' => ['get', 'product/%d'],
        'store' => ['post', 'product'],
        'update' => ['put', 'product/%d'],
        'delete' => ['delete', 'product/%d'],
        'brand_show' => ['get', 'product/brand/%d'],
        'batchstatus'=>['post','product/batchstatus'],
        
        // 商品推荐至首页
        'recommend' => ['post', 'product/recommend/%d'],
        
        'cates' => ['get', 'product/cate'],
        'pattrs' => ['get', 'product/pattr/%d'],
    ];
    
    protected $api_host;
    
    protected $request;
    
    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    
    
    public function indexAction(){
        $this->validate($this->request, [
            'page' => 'integer',
            'name' => 'string',
        ]);
        $search_params = $this->request->all(['goods_name','goods_id','sp_name','is_for_sale','status','first_cat', 'second_cat','product_type','is_index_recommend']);
        
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'id' ,
            'sortedBy'=>'desc',
            'page' => $page,
            'with'=>'shop',
        ];
       
        $ret =  http_admin($this->api_url['index'])->send($params);
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data = $ret['data'];
        }
        
        
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params([]),
            'searchJoin' => 'and',
            'type'=>'all',
        ];
        
        //这里只读类目 不对数据做存redis处理
        $data['cates'] = [];
        if(Redis::exists("categorys")){
            $cateall = Redis::hgetall("categorys");
            if(is_array($cateall)){
                foreach($cateall as $c){
                    $data['cates'] [] = json_decode($c,true);
                }
                $data['cates'] = sortArrByManyField( $data['cates'],'root_id',SORT_ASC,'pid',SORT_ASC,'sort',SORT_ASC,'id',SORT_ASC);
            }
        }else{
            $ret = http_admin($this->api_url['cates'])->send($params);
            if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
                $data['cates'] = $ret['data']['data'];
            }
        }
        $data['cates_json']  = json_encode($data['cates'] );
        $data['search_params'] = $search_params;
        

        return view('Admin.product.goods', paginate($this->request, $data));
    }
    
    public function showAction($id){
        $ret = http_admin($this->api_url['show'],[$id])->send();
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data = $ret['data'];
        }
        
        $data['data']['brand_name'] = "";
        if(isset($data['data']['brand_id'])){
            $brand_ret = http_admin($this->api_url['brand_show'],[$data['data']['brand_id']])->auto(false)->send();
            if(isset($brand_ret['data']['data']['name'])){
                $data['data']['brand_name']= $brand_ret['data']['data']['name'];
            }
        }
        $data['data']['pattrs'] = "";
        if($data['data']['product_type'] == 2){
            $pattrs_ret = http_admin($this->api_url['pattrs'],[$data['data']['id']])->auto(false)->send();
            if(isset($pattrs_ret['data']['attributes'])){
                $data['data']['pattrs'] = explode(";",$pattrs_ret['data']['attributes']);
            }
        }
    
        return view('Admin.product.goods_details', $data);
    }
    
    public function storeAction(){
        
    }
    
    public function updateAction($id){
        $this->validate($this->request, [
            'name' => 'required'
        ]);
        $data = $this->request->all([]);
       
        $ret =  http_admin($this->api_url['update'],[$id])->send($data); 
        
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    public function ajaxUpdateAction($id){
        $data = $this->request->all([]);
        $ret =http_admin($this->api_url['update'],[$id])->send($data); 
        
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    public function ajaxBatchStatusAction(){
        $data = $this->request->all();
        $ret = http_admin($this->api_url['batchstatus'])->send($data);
        if(isset($ret['code']) && $ret['code']===0){
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    
    
    public function deleteAction($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send(); 
        if(isset($ret['code']) && $ret['code']===0){
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    
    
    
    /**
     *  设置商品推荐至首页
     * */
    public function ajaxRecommend($id){
        $data = $this->request->all();
        try {
            if (!isset($data['is_index_recommend'])||$data['is_index_recommend']==''){
                throw new \Exception('请选择是否推荐');
            }if (!isset($data['index_sort'])||$data['index_sort']==''||$data['index_sort']==false||!is_numeric($data['index_sort'])){
                throw new \Exception('请输入正确的排序数字,数字必须大于 0 ');
            }
            $http_data = [
                'is_index_recommend'=>$data['is_index_recommend'],
                'index_sort'=>$data['index_sort'],
            ];
            $ret = http_admin($this->api_url['recommend'], [$id])->send($http_data);

            if(isset($ret['code']) && $ret['code']===0){
                return response()->json(['code'=>0,'message'=>$ret['message']]);
            }else{
                throw new \Exception($ret['message']);
            }
        }catch (\Exception $e){
            return response()->json(['code'=>1,'message'=>$e->getMessage()]);
        }
        
    }
}